package com.bort.system.view;

import com.bort.system.common.DataContainer;
import com.bort.system.model.Goods;
import com.bort.system.utils.ImageCache;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import lombok.NoArgsConstructor;

@NoArgsConstructor
public class GoodsItemView extends FlowPane {

    ImageView imgView;

    Label goodsName;

    Label priceLabel;

    FlowPane info;

    Goods data;

    public GoodsItemView(Goods goodsInfo,boolean lazyLoad) {
        this.data = goodsInfo;
        this.prefHeight(60.0);
        this.prefWidth(164.0);
        this.setMaxWidth(164.0);
        this.setOrientation(Orientation.HORIZONTAL);
        lazyLoad = false;
        if(lazyLoad){
            Image image = new Image(DataContainer.loadImageUrl + goodsInfo.getGoodsImage(),true);
            this.imgView = new ImageView(image);
            ImageCache.setImage(DataContainer.loadImageUrl + goodsInfo.getGoodsImage(),image);
        }else{
            Image image = ImageCache.getImage(DataContainer.loadImageUrl + goodsInfo.getGoodsImage());
            this.imgView = new ImageView(image);
        }
        imgView.setFitHeight(60);
        imgView.setFitWidth(60);
        this.getChildren().add(imgView);

        info = new FlowPane();
        info.setOrientation(Orientation.HORIZONTAL);
        info.setPrefHeight(60);
        info.setPrefWidth(100);
        info.setAlignment(Pos.CENTER_RIGHT);

        goodsName = new Label(goodsInfo.getGoodsName());
        goodsName.setPrefWidth(90);
        goodsName.setPrefHeight(30);
        goodsName.setTextFill(Color.BLACK);
        goodsName.setStyle("-fx-font-size: 16;");
        info.getChildren().add(goodsName);

        String priceStr = null != goodsInfo.getPrice()?goodsInfo.getPrice().toString():"0.00";
        priceLabel = new Label(priceStr+"￥");
        priceLabel.setTextFill(Color.CRIMSON);
        priceLabel.setStyle("-fx-font-size: 16");
        priceLabel.setPrefHeight(30);
        priceLabel.setPrefWidth(90);
        info.getChildren().add(priceLabel);

        this.getChildren().add(info);
    }

    public Goods getData() {
        return data;
    }
}
